load 'lib/date_helper.rb'


class HomeController < ApplicationController
  before_filter :login_required, :except => [:index, :convert, :learn, :demo, :documentation,:failure_test ]
  
  def failure_test
    self.this_should_break()
  end
  
  def index
    if logged_in?
      return(redirect_to :controller => 'home', :action =>'myerrors')
    end
    render :layout => false
  end
  def myerrors
    @errors = current_user.application_errors
    @browser_grouped = {}

    @now = Time.now
    day = 60*60*24
    week = day*7
    month = day*30
    @totals = {'new' => 0, 'accepted'=>0, 'resolved' => 0, 'closed' => 0, 'week' => 0, 'month' => 0, 'total' => 0}
    @columns = ['new','accepted','resolved','closed','week','month','total']
    
    
    for error in @errors
       browser = error.browser ? error.browser : 'unknown'
       unless @browser_grouped[browser]
         @browser_grouped[browser] = {'new' => 0, 'accepted'=>0, 'resolved' => 0, 'closed' => 0, 'week' => 0, 'month' => 0, 'total' => 0}
       end
       @browser_grouped[browser]['total'] += 1
       @totals['total'] += 1
       @browser_grouped[browser][error.status] += 1
       @totals[error.status] += 1
      
       if @now - error.created_at < week
         @browser_grouped[browser]['week'] += 1 
         @totals['week'] += 1
       end
       if @now - error.created_at < month
         @browser_grouped[browser]['month'] += 1
         @totals['month'] += 1
       end
    end
  
    @common = ApplicationError.most_common(current_user)
    @newest = current_user.application_errors(:conditions => 'status = "new"')
  end
  
  def convert
    @site = site
    render :layout => false
  end
end